﻿
function setValidator() {

    // validate signup form on keyup and submit
    $("#form1").validate({
        rules: {
            password: {
                required: true,
                minlength: 5
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            username: {
                required: true,
                minlength: 2
            },
            email: {
                required: true,
                email: true,
                remote: { type: 'POST',
                    mode: 'abort', //防止重复提交，abort前一个
                    url: svcaddr + "IsEmailAvailableForSignup",
                    data: { email: $('#email').val() }
                }
            },
            age: {
                number: true,
                range: [0, 100]
            },
            mage: {
                number: true,
                range: [0, 100]
            },
            sex: {
                required: true
            },

            // for login
            email4login: {
                required: true,
                email: true
            },
            password4login: {
                required: true,
                minlength: 5
            }
        },
        messages: {
            username: {
                required: "请输入您的姓名",
                minlength: "最短2个字"
            },
            password: {
                required: "请输入密码",
                minlength: "最短5个字"
            },
            confirm_password: {
                required: "请输入密码",
                minlength: "最短5个字",
                equalTo: "确认密码须与上述密码相同"
            },
            email:
            {
                required: "请输入邮箱",
                email: "请输入正确的邮箱",
                remote: '此邮箱已经注册，您可以点击"已经注册"使用此邮箱登陆'
            },
            email4login:
            {
                required: "请输入邮箱",
                email: "请输入正确的邮箱",
                remote: '此账号/邮箱不存在，请检查'
            }
        }
    });
}

$(function () {
    if ($.cookie(cookienm_questionnaire_useranswer) == undefined) {
        return dft_redirect("系统无法为您找到答题记录，请返回重新答题", "process.aspx", 2);
    }
    /*
    过滤规则
    $(this.currentForm)
    .find("input, select, textarea")
    .not(":submit, :reset, :image, [disabled]")
    .not( this.settings.ignore )
    .filter(function() {
    !this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);
    // select only the first element for each name, and only those with rules specified
    if ( this.name in rulesCache || !validator.objectLength($(this).rules()) )
    return false;
    rulesCache[this.name] = true;
    return true;
    }); 
    */
    setValidator();
    $('#signin').hide();
    $('#hasacct').toggle(function () {
        $('#signup').hide(); $('#signin').show();
        $(this).text('没有注册'); $('#button').val("登陆并查看结果");
    }, function () {
        $('#signup').show(); $('#signin').hide();
        $(this).text('已经注册'); $('#button').val("注册并查看结果");
    });
    $('#form1').submit(function () {
        if ($('#form1').valid()) {
            // for 注册表单
            if ($('#signup').is(':visible')) {
                var data = {
                    email: $('#email').val(),
                    userName: $('#userName').val(),
                    password: $('#password').val(),
                    sex: $('input[name=sex]:checked').val(),
                    age: $('#age').val(),
                    mage: $('#mage').val(),
                    haschild: $('input[name=haschild]:checked').val(),
                    answer: $.cookie(cookienm_questionnaire_useranswer),
                    questionnaireId: 1
                };

                // 表单填写步骤
                $.ajax({
                    url: svcaddr + "AddUserResultWithSignup",
                    data: JSON.stringify(data),
                    success: function (response) {
                        var newSeqno = response.d;
                        if (newSeqno <= 1) {
                            window.location.href = "/error.aspx?code=-100";
                        }
                        dft_redirect("感谢您的填写，已成功记录，请联系咨询师查询结果，正在为您转向", "/Admin/view.aspx?seqno=" + response.d, 2);
                    },
                    error: function (response) {
                        alert("oops" + response.responseText);
                    }
                });

                return false;
            }
            // for 登陆表单
            else {
                var data = {
                    email: $('#email4login').val(),
                    password: $('#password4login').val(),
                    userAnswer: $.cookie(cookienm_questionnaire_useranswer)
                };

                // 表单填写步骤
                $.ajax({
                    url: svcaddr + "AddUserResultWithSignin",
                    data: JSON.stringify(data),
                    success: function (response) {
                        var newSeqno = response.d;
                        if (newSeqno == -2) {
                            $('#button').after('<label class="error">登陆失败，请重新登陆</label>');
                            return false;
                        }
                        else if (newSeqno == -1) {
                            $('#button').after('<label class="error">未知错导致登陆失败，请重新登陆</label>');
                            return false;
                        }
                        else if (newSeqno > 1) {
                            $.cookie(cookienm_questionnaire_useranswer, null);
                            dft_redirect("感谢您的填写，已成功记录，请联系咨询师查询结果，正在为您转向", "/Admin/view.aspx?seqno=" + response.d, 2);
                        }
                        else {
                            window.location.href = "/error.aspx?code=-100";
                        }
                    }
                });
                return false;
            }
        }
    });
});